package cn.itfcz.server.socket;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

import cn.itfcz.server.http.Request;
import cn.itfcz.server.http.Response;
import cn.itfcz.server.launch.FczTomcat;
import cn.itfcz.server.servlet.HttpFczServlet;

public class SocketProcess extends Thread{

	protected Socket socket;
	
	public SocketProcess(Socket socket) {
		this.socket = socket;
	}
	
	public void run() {
		try {
			Request request = new Request(socket.getInputStream());
			Response response = new Response(socket.getOutputStream());
			
			HttpFczServlet httpFczServlet = FczTomcat.servletMapping.get(request.getUrl().replace("/", ""));
			if(httpFczServlet != null) {
				httpFczServlet.service(request, response);
			}else {
				// 没找到 url 相对应的 servlet
				String res = Response.responseHeader+"没有找到相应的 servlet";
				OutputStream os = socket.getOutputStream();
				os.write(res.getBytes());
				os.flush();
				os.close();
				socket.close();
			}
			
			
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}
